<?php

namespace app\model;

use think\Model;
use app\model\ProjectOpenSourceType;
use app\model\ProjectOpenSource;
use app\model\ProjectCollection;
use app\model\ProjectLike;
use app\model\ProjectComment;
use app\model\User;

class ProjectOpenSource extends Model
{
    protected $table = "ea_project_open_source";

    /**
     * [setWhere 过滤条件]
     * @since  2022-03-25
     * @author Dai哥
     * @param  [type]     $data [description]
     */
    public static function setWhere($data)
    {
        $model = self::whereNull('delete_time')->where('status', 1)->order('is_top DESC');

        # 判断是排序方式
        if(isset($data['sort']) && $data['sort']){
            if($data['sort'] == 'browse'){
                $model = $model->order('browse DESC');
            } else{
                $model = $model->order('create_time DESC');
            }
        } else{
            $model = $model->order('create_time DESC');
        }

        # 所属分类
        if(isset($data['type_id']) && $data['type_id']){
            $model = $model->where('type_id', $data['type_id']);
        }

        return $model;
    }

    /**
     * [getProjectOpenSourceInfo 获取开源项目的详细]
     * @since  2022-03-25
     * @author Dai哥
     * @param  [type]     $data [description]
     * @return [type]           [description]
     */
    public static function getProjectOpenSourceInfo($data)
    {
        $info = self::setWhere($data)
            ->withCount(['like', 'collect', 'evaluate'])
            ->with([
                'user' => function($query) {
                    $query->field('id, nickname, avatar');
                },
                'type' => function($query) {
                    $query->field('id, title, pic, name');
                }
            ])
            ->find($data['id']);

        return $info;
    }

    /**
     * [getProjectList 获取开源项目列表]
     * @since  2022-03-25
     * @author Dai哥
     * @param  [type]     $data [description]
     * @return [type]           [description]
     */
    public static function getProjectList($data)
    {
        $list = self::setWhere($data)
            ->field('id, type_id, is_top, user_id, title, pic, remark, browse, create_time, update_time')
            ->with([
                'user' => function($query) {
                    $query->field('id, nickname, avatar');
                },
                'type' => function($query) {
                    $query->field('id, title, pic, name');
                }
            ])
            ->page($data['page'], $data['limit'] > 20 ? 10 : $data['limit'])
            ->select();

        return $list;
    }

    /**
     * [user 关联作者]
     * @since  2022-03-25
     * @author Dai哥
     * @return [type]     [description]
     */
    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    /**
     * [user 分类]
     * @since  2022-03-25
     * @author Dai哥
     * @return [type]     [description]
     */
    public function type()
    {
        return $this->hasOne(ProjectOpenSourceType::class, 'id', 'type_id');
    }

    /**
     * [like 关联点赞喜欢记录]
     * @since  2022-03-28
     * @author Dai哥
     * @return [type]     [description]
     */
    public function like()
    {
        return $this->hasMany(ProjectLike::class, 'project_id', 'id');
    }

    /**
     * [like 关联评论记录]
     * @since  2022-03-28
     * @author Dai哥
     * @return [type]     [description]
     */
    public function evaluate()
    {
        return $this->hasMany(ProjectComment::class, 'project_id', 'id');
    }

    /**
     * [collect 关联收藏记录]
     * @since  2022-03-28
     * @author Dai哥
     * @return [type]     [description]
     */
    public function collect()
    {
        return $this->hasMany(ProjectCollection::class, 'project_id', 'id');
    }

    # 处理特殊时间
    public function getUpdateTimeAttr($value)
    {
        return date('m月d日 H:i', $value);
    }

    # 处理特殊时间
    public function getCreateTimeAttr($value)
    {
        return date('m月d日 H:i', $value);
    }
}